package civilian;

import java.io.IOException;

import building.Cell;
import jade.core.*;
import jade.domain.FIPAAgentManagement.*;
import jade.lang.acl.*;
import jade.proto.AchieveREInitiator;

public class SendMovePetition {

	AID sender;

	/**
	 * Send by the Civilians to Evacuators to inform they exit the building
	 * 
	 * @throws IOException
	 */
	public void addBehaviour(Agent agent, AID sender, Cell blocking)
			throws IOException {
		System.out.println("sending");
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.setProtocol(agents.UtilsAgents.MOVE);
		msg.setContentObject(blocking);
		msg.setSender(agent.getAID());
		ServiceDescription sd = new ServiceDescription();
		// sd.setType(agents.UtilsAgents.EVACUATOR_AGENT);
		this.sender = sender;
		// msg.addReceiver(agents.UtilsAgents.searchAgent(agent, sd));
		msg.addReceiver(sender);
		agent.addBehaviour(new SendFinishWork(agent, msg));
		System.out.println("end");
	}

	public class SendFinishWork extends AchieveREInitiator {
		/**
	         *
	         */
		private static final long serialVersionUID = 1L;

		public SendFinishWork(Agent arg0, ACLMessage arg1) {
			super(arg0, arg1);
		}

		/**
		 * Receive inform from the manager harvester, and the content is
		 * "Be_scout".
		 */
		@Override
		protected void handleInform(ACLMessage msg) {
			//
			if ((msg.getProtocol().equals(agents.UtilsAgents.MOVE))
					&& (msg.getContent().equals(agents.UtilsAgents.OK))) {
				System.out.println(this.myAgent.getLocalName()
						+ "My movement petition was received by"
						+ msg.getSender());
			} else
				System.out
						.println("Error in protocol in send move, repeat it.");
			// Cal tornar a cridar el protocol load.
		}
	}

}
